/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uca.sisbec.commons;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Jorgore
 */
public class Utils {
    public static final String dateFormat = "dd-MMM-yyyy";
    public static final String dateTime = "dd/MM/yyyy HH:mm";
    public static final String dateAndTimeFormat = "dd-MMM-yyyy HH:mm:ss";
    
    public static java.util.Date getFormattedDate(String dateAsText){
    	SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
        try {
            return formatter.parse(dateAsText);
        } catch (ParseException ex) {
            return null;
        }
    }
    
    public static java.sql.Date getSqlDate(java.util.Date date){
    	return new java.sql.Date(date.getTime());
    }
    
    public static java.sql.Date getSqlDate(String date) {
    	return new java.sql.Date(getFormattedDate(date).getTime());
    }
    
    public static String getStringSqlDate(java.sql.Date date){
        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
        return formatter.format(date).toUpperCase();
    }
    
    public static String getStringUtilDate(java.util.Date date){
        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
        return formatter.format(date).toUpperCase();
    }
    
    public static String now(){
        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
        return formatter.format(new java.util.Date());
    }
    
    public static String nowDatePicker(){
        SimpleDateFormat formatter = new SimpleDateFormat(dateTime);
        return formatter.format(new java.util.Date());
    }
    
    public static boolean isNullOrEmpty(Object obj){
        if(obj==null)return true;
        if(obj.toString().trim().equals(""))return true;
        return false;
    }
    
    public static String encriptarMD5(String cad){
        String res = "";
        try {
            byte[] bytesOfMessage = cad.getBytes("UTF-8");
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] thedigest = md.digest(bytesOfMessage);
            BigInteger bigInt = new BigInteger(1, thedigest);
            res = bigInt.toString(16);
            while(res.length() < 32 ){
                res = "0" + res;
            }
            return res;
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static String getStringUtilDateAndTime(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat(dateAndTimeFormat);
        return formatter.format(date).toUpperCase();
    }
    
    public static java.util.Date getUtilDatePicker(String date){
        SimpleDateFormat formatter = new SimpleDateFormat(dateTime);
        try {
            return formatter.parse(date);
        } catch (ParseException ex) {
            return null;
        }
    }
    
    public static String getUtilDatePicker(java.util.Date date){
        SimpleDateFormat formatter = new SimpleDateFormat(dateTime);
        return formatter.format(date);
    }
    
    /*public static void main (String[] args){
        //System.out.println("hola");
        //System.out.println(encriptarMD5("Juanch0S"));
        java.util.Date date = new java.util.Date();
        java.sql.Date sql = new java.sql.Date(date.getTime());
        System.out.println(getStringSqlDate(sql));
    }*/
}
